using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using CustomWPFColorPicker;

namespace XmlStudio.Controls {
    // ReSharper disable RedundantExtendsListEntry
    public partial class ColorPickerControlView : UserControl {
        // ReSharper restore RedundantExtendsListEntry
        public SolidColorBrush CurrentColor {
            get { return (SolidColorBrush)GetValue(CurrentColorProperty); }
            set { SetValue(CurrentColorProperty, value); }
        }

        public static DependencyProperty CurrentColorProperty =
            DependencyProperty.Register("CurrentColor", typeof(SolidColorBrush), typeof(ColorPickerControlView), new PropertyMetadata(Brushes.Black));

        public static RoutedUICommand SelectColorCommand = new RoutedUICommand("SelectColorCommand", "SelectColorCommand", typeof(ColorPickerControlView));
        private Window advancedPickerWindow;

        public ColorPickerControlView() {
            DataContext = this;
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(SelectColorCommand, SelectColorCommandExecute));
        }

        private void SelectColorCommandExecute(object sender, ExecutedRoutedEventArgs e) {
            CurrentColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString(e.Parameter.ToString()));
        }

        private static void ShowModal(Window advancedColorWindow) {
            advancedColorWindow.Owner = Application.Current.MainWindow;
            advancedColorWindow.ShowDialog();
        }

        void AdvancedPickerPopUpKeyDown(object sender, KeyEventArgs e) {
            if(e.Key == Key.Escape)
                advancedPickerWindow.Close();
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            popup.IsOpen = false;
            e.Handled = false;
        }

        private void MoreColorsClicked(object sender, RoutedEventArgs e) {
            popup.IsOpen = false;
            var advancedColorPickerDialog = new AdvancedColorPickerDialog {
                currentColorBorder = { Background = this.CurrentColor }
            };
            advancedPickerWindow = new Window {
                AllowsTransparency = true,
                Content = advancedColorPickerDialog,
                WindowStyle = WindowStyle.None,
                ShowInTaskbar = false,
                Background = new SolidColorBrush(Colors.Transparent),
                Padding = new Thickness(0),
                Margin = new Thickness(0),
                WindowState = WindowState.Normal,
                WindowStartupLocation = WindowStartupLocation.CenterOwner,
                SizeToContent = SizeToContent.WidthAndHeight,
                ShowActivated = true,
                Topmost = true
            };

            advancedPickerWindow.DragMove();
            advancedPickerWindow.KeyDown += AdvancedPickerPopUpKeyDown;
            advancedColorPickerDialog.DialogResultEvent += AdvancedColorPickerDialogDialogResultEvent;
            advancedColorPickerDialog.Drag += AdvancedColorPickerDialogDrag;
            ShowModal(advancedPickerWindow);
        }

        void AdvancedColorPickerDialogDrag(object sender, DragDeltaEventArgs e) {
            advancedPickerWindow.DragMove();
        }

        void AdvancedColorPickerDialogDialogResultEvent(object sender, EventArgs e) {
            advancedPickerWindow.Close();
            var dialogEventArgs = (DialogEventArgs)e;
            if(dialogEventArgs.DialogResult == DialogResult.Cancel)
                return;
            CurrentColor = dialogEventArgs.SelectedColor;
        }
    }
}